package org.jeecg.modules.system.util;

import org.apache.commons.lang.StringUtils;
import org.jeecg.system.common.entity.ChangeLogs;
import org.jeecg.system.common.icomm.FieldName;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ChangeLog {

    /**
     * 修改前 修改后字段对比
     * 属性要加上@fileName注解
     * @param t
     * @param old
     * @param newObj
     * @param <T>
     * @return
     */
    public static <T> List<ChangeLogs> check(T t, Object old, Object newObj, String fName){
        T pojo1 =(T) old;
        T pojo2 =(T) newObj;
        Class classes = pojo1.getClass();
        Field[] fields = pojo1.getClass().getDeclaredFields();
        List<ChangeLogs> result = new ArrayList<>();
        for (Field field : fields) {
            if ("serialVersionUID".equals(field.getName())) {
                continue;
            }
            try {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), classes);
                Method readMethod = pd.getReadMethod();
                Object o1 = readMethod.invoke(pojo1);
                Object o2 = readMethod.invoke(pojo2);
                if(o1==null&&o2==null){
                    continue;
                }else if(o1==null&&o2!=null){
                    ChangeLogs changeValue = getChangeValue(o1, o2, field, fName);
                    if(changeValue==null){
                        continue;
                    }
                    result.add(changeValue);
                }else if(o1!=null&&o2==null){
                    ChangeLogs changeValue = getChangeValue(o1, o2, field, fName);
                    if(changeValue==null){
                        continue;
                    }
                    result.add(changeValue);
                }else
                if(!o1.toString().equals(o2.toString())){
                    ChangeLogs changeValue = getChangeValue(o1, o2, field, fName);
                    if(changeValue==null){
                        continue;
                    }
                    result.add(changeValue);
                }
            } catch (IntrospectionException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return  result;
    }

    static ChangeLogs  getChangeValue(Object oldobj,Object newobj,Field field,String fName){
        ChangeLogs logs = new ChangeLogs();
        FieldName fieldName = field.getAnnotation(FieldName.class);

        logs.setFieldsName(fieldName == null ? field.getName() : fieldName.value());//中文名称
        logs.setUpdFields(field.getName());
        if("createBy".equals(field.getName())||"createTime".equals(field.getName())
                ||"updateTime".equals(field.getName())||"updateBy".equals(field.getName())){
            return null;
        }
        logs.setOldValue(oldobj == null ? "null" : oldobj.toString());
        logs.setNewValue(newobj == null ? "null" : newobj.toString());
        logs.setUpdModules(fName);//模块名字
        //result.add(strings);
        return logs;
    }
}
